<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"
	contentType="text/html; charset=UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>用户管理</title>
<link rel="stylesheet" type="text/css"
	href="<%=path%>/jquery-easyui/themes/default/easyui.css">
<link rel="stylesheet" type="text/css"
	href="<%=path%>/jquery-easyui/themes/icon.css">
<script type="text/javascript"
	src="<%=path%>/jquery-easyui/jquery-1.8.0.js"></script>
<script type="text/javascript"
	src="<%=path%>/jquery-easyui/jquery.easyui.min.js"></script>
<script type="text/javascript"
	src="<%=path%>/jquery-easyui/locale/easyui-lang-zh_CN.js"></script>
<script type="text/javascript" src="<%=path%>/js/jquery.json-2.4.js"></script>
<script type="text/javascript" src="<%=path%>/js/post.js"></script>
<script type="text/javascript">
	function setAdmin() {
		var grid = $('#grid');
		var entity = grid.datagrid('getSelected');
		if (entity == null) {
			$.messager.alert('提示','请选择数据!');
			return;
		}

		$.messager.confirm('询问', '确定要' 
			+ (entity.isAdmin ? '取消' : '设置') 
			+ '"' + entity.name + '"考勤员吗?', function(r) {
			if (!r) {
				return;
			}

			$.messager.progress({
				title : '系统提示',
				msg : '处理中，请稍候...'
			});
			var path = "${pageContext.request.contextPath}";
			var url = path + '/user/save/';
			$.ajax({
				type : "POST",
				url : url,
				data : "id=" + entity.employeeId + '&isAdmin=' + (!entity.isAdmin),
				success : function(data) {
					$.messager.progress('close');
					if (data.isSucess) {
						$.messager.show({
							title : '系统提示',
							msg : '处理成功！',
							timeout : 2000,
							showType : 'slide'
						});
						$('#grid').datagrid('reload');
					}
					 else {
						$.messager.alert('错误', data.message, 'error');
					}
				},
				error : function() {
					$.messager.progress('close');
					$.messager.alert('错误', '无法处理！', 'error');
				}
			});
		});
	}

	function reset() {
		var grid = $('#grid');
		var entity = grid.datagrid('getSelected');
		if (entity == null) {
			$.messager.alert('提示','请选择数据!');
			return;
		}
		$.messager.confirm('询问', '确定要重置' + '"' + entity.name + '"的密码吗?', function(r) {
			if (!r) {
				return;
			}
			$.messager.progress({
				title : '系统提示',
				msg : '处理中，请稍候...'
			});
			var path = "${pageContext.request.contextPath}";
			var url = path + '/user/reset/';
			$.ajax({
				type : "POST",
				url : url,
				data : "id=" + entity.employeeId,
				success : function(data) {
					$.messager.progress('close');
					if (data.isSucess) {
						$.messager.show({
							title : '系统提示',
							msg : '处理成功！',
							timeout : 2000,
							showType : 'slide'
						});
						$('#grid').datagrid('reload');
					}
					 else {
						$.messager.alert('错误', data.message, 'error');
					}
				},
				error : function() {
					$.messager.progress('close');
					$.messager.alert('错误', '无法处理！', 'error');
				}
			});
		});
	}
	
	function formatBoolean(val, row){
		return val ? '是' : '否';
	}
	
	function cellStyler(val, row){
		if (val) {
			return 'color:green;';
		}
	}
	
	function onLoadSuccessTree(row, data) {
		var tree = $('#tree');
		var root = tree.treegrid('getRoot');
		if (!root) {
			return;
		}
		tree.treegrid('select',root.id);
		loadGird();
	}

	function loadGird() {
		var tree = $('#tree');
		var row = tree.treegrid('getSelected');

		if (!row) {
			return;
		}
		var grid = $('#grid');
		grid.datagrid('load', {
			id : row.id,
			key : $('#searchKey').val()
		});
	}
</script>
</head>
<body class="easyui-layout">
	<!-- 部门 -->
	<div data-options="region:'west',split:true" style="width:160px;">
		<table id="tree" class="easyui-treegrid"
			data-options="
				fit:true,
				url: '<%=path%>/department/loadAll',
				idField: 'id',
				treeField: 'name',
				onLoadSuccess: onLoadSuccessTree,
				onClickRow: loadGird,
				queryParams:{id:2}
			">
			<thead>
				<tr>
					<th data-options="field:'name'" width="140">部门名称</th>
				</tr>
			</thead>
		</table>
	</div>

	<!-- 工具栏 -->
	<div id="tb" style="height:auto">
		<a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true"
			onclick="setAdmin()">设置考勤员</a> <a href="#" class="easyui-linkbutton"
			iconCls="icon-add" plain="true" onclick="reset()">重置密码</a>
		&nbsp&nbsp&nbsp&nbsp检索条件: <input class="easyui-validatebox"
			type="text" id="searchKey" onchange="loadGird(this.value)"
			data-options="required:false" />
	</div>

	<div data-options="region:'center'">
		<table id="grid" class="easyui-datagrid"
			data-options="
			sortOrder:'desc',
			sortName:'employeeCode',
			remoteSort:false,
			idField:'id',
			fit:true,
			singleSelect:true,
			collapsible:true,
			url:'<%=path%>/user/loadAll/',
			toolbar:'#tb',
			striped: true,
			rownumbers:true">
			<thead>
				<tr>
					<th data-options="field:'employeeCode',width:160,sortable:true">工号</th>
					<th data-options="field:'name',width:180,sortable:true">姓名</th>
					<th
						data-options="field:'isAdmin',width:180,sortable:true,styler:cellStyler,formatter:formatBoolean">是否为考勤员</th>
				</tr>
			</thead>
		</table>

	</div>
</body>
</html>